package com.automannn;

import com.automannn.model.Product;
import com.automannn.repository.ProductRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        // 使用XML配置方式
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 或者使用Java配置方式
        // ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        ProductRepository repository = context.getBean(ProductRepository.class);

        // 创建一些测试数据
        Product product1 = new Product("Laptop", 999.99, 10, "Electronics");
        Product product2 = new Product("Smartphone", 699.99, 20, "Electronics");
        Product product3 = new Product("Book", 19.99, 50, "Education");

        // 保存产品
        repository.save(product1);
        repository.save(product2);
        repository.save(product3);

        System.out.println("Products saved successfully!");

        // 通过名称查找
        List<Product> laptops = repository.findByName("Laptop");
        System.out.println("Found " + laptops.size() + " laptops:");
        laptops.forEach(System.out::println);

        // 通过类别查找
        List<Product> electronics = repository.findByCategory("Electronics");
        System.out.println("Found " + electronics.size() + " electronic products:");
        electronics.forEach(System.out::println);

        // 通过价格范围查找
        List<Product> expensiveProducts = repository.findByPriceBetween(500.0, 1000.0);
        System.out.println("Found " + expensiveProducts.size() + " expensive products:");
        expensiveProducts.forEach(System.out::println);

        // 统计所有产品
        long count = repository.count();
        System.out.println("Total products in index: " + count);

        // 维持运行
        System.out.println("Press any key to exit...");
        try {
            System.in.read();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("Exiting...");

        ((ClassPathXmlApplicationContext) context).close();
    }
}
